#!/usr/bin/env python

import socket

class timeval:
    def __init__(self):
        tv_sec = 0
        tv_usec = 0;

def gettimeofday():
    from time import time

    tv = timeval()
    ts = time()
    tv.tv_sec = int(ts)
    tv.tv_usec = int((ts - tv.tv_sec) * 1000000)

    return tv

ADDR = ("0.0.0.0", 46420)

# create a UDP socket and listening at ADDR
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(ADDR)

while True:

    # network IO
    data, addr = s.recvfrom(1024)
    ts = gettimeofday()
    print "[%10d.%5d] receving %d bytes" % (ts.tv_sec, ts.tv_usec, len(data))


    import struct
    buf = struct.pack(">II%ds" % (len(data) - 8),
                ts.tv_sec, ts.tv_usec, data[8:])
    s.sendto(buf, addr)
